
PLUG.Platform = PLUG_PLATFORM_CLIENT

PLUG.ClassName = "cl_servercommands"
PLUG.PrintName = "Server Commands Interface"
PLUG.Author = "thomasfn"

local serveractions = {
	{ "scoms", "Server Commands" }
}
function PLUG:GetServerActions()
	return serveractions
end

local ENTRY_TEXT = 1
local ENTRY_ONOFF = 2

local scoms = {
	Gravity = { "sv_gravity", ENTRY_TEXT },
	Noclip = { "sbox_noclip", ENTRY_ONOFF },
	["PVP Damage"] = { "sbox_plpldamage", ENTRY_ONOFF },
	["Godmode"] = { "sbox_godmode", ENTRY_ONOFF },
}

function PLUG:GetServerCommandList()
	return scoms
end

function PLUG:BuildActionPanel( panel, action )
	if (action == "scoms") then
		local w, h = panel:GetSize()
		
		local lst = vgui.Create( "DPanelList", panel )
		lst:SetPos( 5, 5 )
		lst:SetSize( w-10, h-10 )
		lst:SetPadding( 5 )
		lst:SetSpacing( 0 )
		lst:EnableHorizontal( false )
		lst:EnableVerticalScrollbar( true )
		
		local scomlist = plugs.MergeCallOverride( "GetServerCommandList" )
		for k, v in pairs( scomlist ) do
			local pn = self:BuildEntryRow( k, v[1], v[2] )
			lst:AddItem( pn )
		end
	end
end

local bgcol = Color( 255, 255, 255, 50 )
function PLUG:BuildEntryRow( name, convar, entrymode )
	entrymode = entrymode or ENTRY_TEXT

	local pn = vgui.Create( "DPanel" )
	function pn:Paint()
		surface.SetDrawColor( 255, 255, 255, 255 )
		surface.DrawRect( 0, self:GetTall() - 1, self:GetWide(), 1 )
	end
	
	local lbl = vgui.Create( "DLabel", pn )
	lbl:SetText( name .. ":" )
	lbl:SizeToContents()
	
	local cvar = GetConVar( convar )
	
	if (entrymode == ENTRY_TEXT) then
	
		local txt = vgui.Create( "DTextEntry", pn )
		if (cvar) then txt:SetText( cvar:GetString() ) end
		
		local btn = vgui.Create( "DButton", pn )
		btn:SetText( "Apply" )
		function btn:DoClick()
			plugs.Call( "SendCommand", "set_cvar", convar, txt:GetValue() )
		end
		
		function pn:PerformLayout()
			self:SetTall( 24 )
			local w, h = self:GetSize()
			lbl:SizeToContents()
			lbl:SetPos( 0, (h*0.5) - (lbl:GetTall()*0.5) )
			btn:SizeToContents()
			btn:SetSize( btn:GetWide() + 10, 20 )
			btn:SetPos( w - 5 - btn:GetWide(), 2 )
			txt:SetPos( lbl:GetWide()+5, 2 )
			txt:SetSize( w - lbl:GetWide() - btn:GetWide() - 20, 20 )
		end
		
	end
	
	if (entrymode == ENTRY_ONOFF) then
	
		local btna = vgui.Create( "DImageButton", pn )
		btna:SetImage( "gmanage/ico_accept" )
		function btna:DoClick()
			plugs.Call( "SendCommand", "set_cvar", convar, "1" )
		end
		
		local btnb = vgui.Create( "DImageButton", pn )
		btnb:SetImage( "gmanage/ico_deny" )
		function btnb:DoClick()
			plugs.Call( "SendCommand", "set_cvar", convar, "0" )
		end
		
		local t = "OFF"
		if (cvar:GetBool()) then t = "ON" end
		lbl:SetText( name .. " [" .. t .. "]" )
		
		btna.Paint2 = btna.Paint
		function btna:Paint()
			if (t == "ON") then draw.RoundedBox( 4, 0, 0, self:GetWide(), self:GetTall(), bgcol ) end
			self:Paint2()
		end
		
		btnb.Paint2 = btnb.Paint
		function btnb:Paint()
			if (t == "OFF") then draw.RoundedBox( 4, 0, 0, self:GetWide(), self:GetTall(), bgcol ) end
			self:Paint2()
		end
		
		function pn:PerformLayout()
			self:SetTall( 70 )
			local w, h = self:GetSize()
			lbl:SizeToContents()
			lbl:SetPos( 0, (h*0.5) - (lbl:GetTall()*0.5) )
			btna:SetSize( 64, 64 )
			btna:SetPos( (w*0.5) - 96, 3 )
			btnb:SetSize( 64, 64 )
			btnb:SetPos( (w*0.5) + 32, 3 )
		end
	
	end
	
	return pn
end